package com.tronsis.distribution.biz.impl;

import android.app.Activity;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tronsis.distribution.bean.AskBuyBean;
import com.tronsis.distribution.bean.AskBuyDetailBean;
import com.tronsis.distribution.bean.BuyRequestBean;
import com.tronsis.distribution.bean.CouponBean;
import com.tronsis.distribution.bean.DeliveryBean;
import com.tronsis.distribution.bean.DeliveryDetailBean;
import com.tronsis.distribution.bean.LowerStockBean;
import com.tronsis.distribution.bean.MyStockBean;
import com.tronsis.distribution.bean.ScanDeliveryReq;
import com.tronsis.distribution.bean.StockLogBean;
import com.tronsis.distribution.biz.OrderBiz;
import com.tronsis.distribution.http.Url;
import com.tronsis.distribution.pay.wechatpay.WeChatPayOrderBean;
import com.tronsis.library.http.HttpCallBack;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.RequestClient;
import com.tronsis.library.http.RequestMethod;
import com.tronsis.library.http.UICallBack;

import java.util.List;


public class OrderBizImpl implements OrderBiz {


    @Override
    public void getAskBuyList(Activity activity, int page, int size, String state, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<BuyRequestBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("page", String.valueOf(page), "size", String.valueOf(size), "state", state).excute(RequestMethod.GET, Url.ASK_BUY_OPTION);
    }

    @Override
    public void askBuyPass(Activity activity, String buyId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter().excute(RequestMethod.POST, Url.ASK_BUY_OPTION + "/" + buyId + "/pass");

    }

    @Override
    public void askBuyReject(Activity activity, String buyId, String refundFid, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("refundFid", refundFid).excute(RequestMethod.POST, Url.ASK_BUY_OPTION + "/" + buyId + "/reject");
    }

    @Override
    public void getMyBuyList(Activity activity, int page, int size, String state, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<AskBuyBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("page", String.valueOf(page), "size", String.valueOf(size), "state", state).excute(RequestMethod.GET, Url.MY_BUY);

    }

    @Override
    public void getMyBuyDetail(Activity activity, String buyId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, AskBuyDetailBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.MY_BUY + "/" + buyId);
    }

    @Override
    public void deleteMyBuy(Activity activity, String buyId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.DELETE, Url.MY_BUY + "/" + buyId);
    }

    @Override
    public void buyPay(Activity activity, String buyId, String payeeId, String paymentFid, String points, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        })
                .addParameter("payeeId", payeeId, "paymentFid", paymentFid, "points", points)
                .excute(RequestMethod.POST, Url.MY_BUY + "/" + buyId + "/pay");
    }

    @Override
    public void getCouponList(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, CouponBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.COUPON);

    }

    @Override
    public void useCoupon(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter().excute(RequestMethod.POST, Url.COUPON);
    }

    @Override
    public void getDeliveryList(Activity activity, int page, int size, String state, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<DeliveryBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("page", String.valueOf(page), "size", String.valueOf(size), "state", state).excute(RequestMethod.GET, Url.MY_DELIVERY);

    }

    @Override
    public void getDeliveryDetail(Activity activity, String deliveryId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, DeliveryDetailBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.MY_DELIVERY + "/" + deliveryId);
    }

    @Override
    public void qrScanDelivery(Activity activity, ScanDeliveryReq scanDeliveryReq, final UICallBack callBack) throws Exception {
        String json = new Gson().toJson(scanDeliveryReq);
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addJsonParameter(json).excute(RequestMethod.POST, Url.DELIVERY_SCAN);
    }

    @Override
    public void getMyStockList(Activity activity, int page, int size, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<MyStockBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("page", String.valueOf(page), "size", String.valueOf(size)).excute(RequestMethod.GET, Url.MY_STOCK);
    }

    @Override
    public void applyDelivery(Activity activity, String addrId, List list, final UICallBack callBack) throws Exception {
        String mapStr = new Gson().toJson(list);
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addJsonParameter(mapStr).excute(RequestMethod.POST_JSON, Url.STOCK_DELIVERY_APPLY + "?addrId=" + addrId);

    }


    @Override
    public void getStockLog(Activity activity, int page, int size, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<StockLogBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("page", String.valueOf(page), "size", String.valueOf(size)).excute(RequestMethod.GET, Url.STOCK_LOG);
    }

    public void getStockTotalPrice(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, Double.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.STOCK_TOTAL_PRICE);
    }

    @Override
    public void wxPay(Activity activity, String addrId , String buyId, String points, final UICallBack callBack) throws Exception {
        if(TextUtils.isEmpty(addrId)){
            RequestClient.getInstance(new HttpCallBack(activity, WeChatPayOrderBean.class) {
                @Override
                public void onStart() {
                    callBack.onStart();
                }

                @Override
                public void onCallBack(int status, Object result) {
                    switch (status) {
                        case 200:
                            callBack.onRefresh(result);
                            break;
                        default:
                            callBack.onFailure(status);
                            break;
                    }
                }
            }).addParameter("buyId", buyId, "points", points).excute(RequestMethod.POST, Url.WX_PAY);
        }else{
            RequestClient.getInstance(new HttpCallBack(activity, WeChatPayOrderBean.class) {
                @Override
                public void onStart() {
                    callBack.onStart();
                }

                @Override
                public void onCallBack(int status, Object result) {
                    switch (status) {
                        case 200:
                            callBack.onRefresh(result);
                            break;
                        default:
                            callBack.onFailure(status);
                            break;
                    }
                }
            }).addParameter("addrId", addrId, "buyId", buyId, "points", points).excute(RequestMethod.POST, Url.WX_PAY);
        }
    }

    @Override
    public void wxNotify(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter().excute(RequestMethod.POST, Url.WX_NOTIFY);

    }

    @Override
    public void queryOrder(Activity activity, String buyId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("buyId", buyId).excute(RequestMethod.POST, Url.WX_QUERY_ORDER);

    }

    @Override
    public void getLowerStockList(Activity activity, String id, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<List<LowerStockBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("id", id).excute(RequestMethod.GET, Url.LOWER_STOCK);
    }

    @Override
    public void getProfit(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, Double.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.UPGRADE_PROFIT);
    }
}
